package com.deyuanyun.pic.pipe.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.ExceptionCodeUtil;
import com.deyuanyun.pic.common.exception.bizcode.basedata.pipe.PipeValveCodeEnum;
import com.deyuanyun.pic.common.exception.code.ModuleMsg;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.domain.base.UnifiedEncoding;
import com.deyuanyun.pic.mapper.base.UnifiedEncodingMapper;
import com.deyuanyun.pic.pipe.domain.exceldomain.PipeValveImportDemo;
import com.deyuanyun.pic.pipe.mapper.DyyPipeLineMapper;
import com.deyuanyun.pic.pipe.mapper.ValveMapper;
import com.deyuanyun.pic.pipe.service.PipeValveService;
import com.deyuanyun.pic.pipe.support.utils.UniformCodeUtils;
import com.deyuanyun.pic.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @ClassName PipeValveServiceImpl
 * @Description 管道基础数据 ->阀门service实现
 * @author 王敏
 * @date 2015年1月18日
 */
@Service
public class PipeValveServiceImpl implements PipeValveService {

	/**
	 * 诸如一个MAP
	 */
	@Resource
	private ValveMapper valveMapper;
	
	@Resource
	private PipeSegmentService pipeSegmentService;
	
	@Resource
	private UnifiedEncodingMapper unifiedEncodingMapper;
	
	@Resource
	private DyyPipeLineMapper dyyPipeLineMapper;

	@Override
	public DTOResult getPipeValveByParam(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage,
			BigDecimal endMileage, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub

		int page = 0;

		int size = 0;
		// 初始化当前页数
		if (pageNum == null) {
			page = 0;
		} else {
			page = pageNum;
		}
		// 初始化每页显示数量
		if (pageSize == null) {
			size = 20;
		} else {
			size = pageSize;
		}

		PageHelper.startPage(page, size);

		String code = "";

		if (null == pipeLineId || "".equals(pipeLineId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.pipeLinedIdIsNull.getCode(),
					"未选择管线");
		}

		if (null == pipeSegmentId || "".equals(pipeSegmentId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.pipeSegmentIdIsNull.getCode(),
					"未选择管线");
		}

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && "".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		Page<HashMap<String, Object>> map = null;

		// 在DAO层获取一个MAP集合对象
		map = valveMapper.selectBaseByParam(pipeLineId, pipeSegmentIdStr,
				mileageStart, mileageEnd);

		if (map == null) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.mapperIsNull.getCode(),
					"没有查询到结果集");
		}

		if (map.size() <= 0) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.mapperIsNull.getCode(),
					"没有查询到结果集");
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(), "正常", map);

	}

	@Override
	public List<List<String[]>> getPipeValveToExcel(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage, BigDecimal endMileage) {

		
		// 定义一个返回值
		List<List<String[]>> result = new ArrayList<>();

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && "".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		List<LinkedHashMap<String, Object>> list = valveMapper
				.selectValveToExcel(pipeLineId, pipeSegmentIdStr, mileageStart,
						mileageEnd);
		List<LinkedHashMap<String, Object>> list2=new ArrayList<LinkedHashMap<String, Object>>();

		for(int i=0;i<list.size();i++){
			LinkedHashMap<String, Object> map=list.get(i);
			map.put("VALVE_STATUS", CodeUtil.getName((String) map.get("VALVE_STATUS")));
			map.put("VALVE_TYPE", CodeUtil.getName((String) map.get("VALVE_TYPE")));
			map.put("OPERATOR_TYPE", CodeUtil.getName((String) map.get("OPERATOR_TYPE")));
			map.put("AUTOMATED", CodeUtil.getName((String) map.get("AUTOMATED")));
			map.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("INLET_CONNECTION_TYPE")));
			map.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("OUTLET_CONNECTION_TYPE")));
			map.put("VALVEFUNCTION", CodeUtil.getName((String) map.get("VALVEFUNCTION")));
			map.put("INDREMOTECON", CodeUtil.getName((String) map.get("INDREMOTECON")));
			map.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String) map.get("REPAIR_SUGGESTIONG")));
			map.put("REPAIR_STATE", CodeUtil.getName((String) map.get("REPAIR_STATE")));
			list2.add(map);
		}
		
		
		
		if (list2.size() == 0) {
			list2 = null;
		}

		result.add(JXL.toList(list2, "yyyy-MM-dd HH:mm:ss"));

		return result;
	}


	@Override
	public Page<HashMap<String, Object>> getDetailed(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage,
			BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		
		int page = 1;

		int size = 10;

		if (null != pageNum ) {
			page = pageNum;
		}

		if (null != pageSize ) {
			size = pageSize;
		}

		PageHelper.startPage(page,size);
		
		
		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && !"".equals(endMileage)) {
			mileageEnd = endMileage;
		}
		Page<HashMap<String, Object>> list = new Page<HashMap<String, Object>>();
		Page<HashMap<String, Object>> list2 = new Page<HashMap<String, Object>>();

		list=valveMapper.selectValveDetailed(pipeLineId, pipeSegmentId, mileageStart, mileageEnd);
		for (int i=0;i<list.size();i++){
			HashMap map=list.get(i);
			map.put("VALVE_STATUS", CodeUtil.getName((String) map.get("VALVE_STATUS")));
			map.put("VALVE_TYPE", CodeUtil.getName((String) map.get("VALVE_TYPE")));
			map.put("OPERATOR_TYPE", CodeUtil.getName((String) map.get("OPERATOR_TYPE")));
			map.put("AUTOMATED", CodeUtil.getName((String) map.get("AUTOMATED")));
			map.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("INLET_CONNECTION_TYPE")));
			map.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("OUTLET_CONNECTION_TYPE")));
			map.put("VALVEFUNCTION", CodeUtil.getName((String) map.get("VALVEFUNCTION")));
			map.put("INDREMOTECON", CodeUtil.getName((String) map.get("INDREMOTECON")));
			map.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String) map.get("REPAIR_SUGGESTIONG")));
			map.put("REPAIR_STATE", CodeUtil.getName((String) map.get("REPAIR_STATE")));
			list2.add(map);
		}
		list2.setTotal(list.getTotal());
		list2.setPageNum(list.getPageNum());
		list2.setPageSize(list.getPageSize());
		
		
		
		return list2;
	}

	@Override
	public boolean importData(List<PipeValveImportDemo> data) throws  Exception {
		
		
		
		data=UniformCodeUtils.sortedByLine(data,"pipeLineId","","code","阀门");
		Map<String, Object> bean = null;
    	for(PipeValveImportDemo info:data)
	    {
    		String valueId=UUID.randomUUID().toString().replaceAll("-", "");
    		
    		
			bean = MapUtil.beanToMap(info);
			System.out.println(bean.get("recordtime"));
			bean.put("id", valueId);
			bean.put("createDate", StringUtils.convertDateToString(
					new Date(), StringUtils.DATE_TIME_DEFAULT));
			bean.put("createUser", "");
			bean.put("valueId", valueId);
			valveMapper.insertData(bean);
			valveMapper.insertPositData(bean);
			
	    }
	    
		return true;
	}

	@Override
	public Page<HashMap<String, Object>> findPage(HashMap param) {
		String valveCodeId =CodeUtil.getCodeId("SITE_TYPE", "阀室");
		param.put("valveCodeId", valveCodeId);
		Page<HashMap<String, Object>> data=valveMapper.findPage(param);
		
		
		for(HashMap<String, Object> m:data)
		{
			m.put("VALVE_STATUS", CodeUtil.getName((String)m.get("VALVE_STATUS")));
			m.put("VALVE_TYPE", CodeUtil.getName((String)m.get("VALVE_TYPE")));
			m.put("OPERATOR_TYPE", CodeUtil.getName((String)m.get("OPERATOR_TYPE")));
			m.put("AUTOMATED", CodeUtil.getName((String)m.get("AUTOMATED")));
			m.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("INLET_CONNECTION_TYPE")));
			m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
			m.put("VALVEFUNCTION", CodeUtil.getName((String)m.get("VALVEFUNCTION")));
			m.put("INDREMOTECON", CodeUtil.getName((String)m.get("INDREMOTECON")));
			m.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String)m.get("REPAIR_SUGGESTIONG")));
			m.put("REPAIR_STATE", CodeUtil.getName((String)m.get("REPAIR_STATE")));
			
		}
		
		MapUtil.emptyConvert(data);
		
		
		
		
		return data;
	}

	@Override
	public List<List<String[]>> excelExport(Map map) {
		String valveCodeId =CodeUtil.getCodeId("SITE_TYPE", "阀室");
		map.put("valveCodeId", valveCodeId);
		List<LinkedHashMap<String, Object>> data = valveMapper.exportExcel(map);
		for(HashMap<String, Object> m:data)
		{
			m.put("VALVE_STATUS", CodeUtil.getName((String)m.get("VALVE_STATUS")));
			m.put("VALVE_TYPE", CodeUtil.getName((String)m.get("VALVE_TYPE")));
			m.put("OPERATOR_TYPE", CodeUtil.getName((String)m.get("OPERATOR_TYPE")));
			m.put("AUTOMATED", CodeUtil.getName((String)m.get("AUTOMATED")));
			m.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("INLET_CONNECTION_TYPE")));
			m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
			m.put("VALVEFUNCTION", CodeUtil.getName((String)m.get("VALVEFUNCTION")));
			m.put("INDREMOTECON", CodeUtil.getName((String)m.get("INDREMOTECON")));
			m.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String)m.get("REPAIR_SUGGESTIONG")));
			m.put("REPAIR_STATE", CodeUtil.getName((String)m.get("REPAIR_STATE")));
			
		}
		MapUtil.emptyConvertLink(data);
		List<List<String[]>> result=new ArrayList<List<String[]>>();
		List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd HH:mm:ss");
		result.add(lsr);
		return result;
	}

	public int delGis(Map map){
		return valveMapper.delGis(map);
	}
	public int delData(Map map){
		valveMapper.delGis(map);
		valveMapper.delData(map);
		return 1;
	}

}
